import { getDeviceGroupTreeNodeExclude, readDeviceNodes } from '@/api/iot/admin/device';
import { AdColumn } from '@/components/AdVxeTable';
import { useI18n } from '@/hooks/web/useI18n';

const { t } = useI18n();

export const columns: AdColumn[] = [
  {
    title: t('entity.idTitle'),
    field: 'Id',
    width: 180,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'integer',
  },
  {
    title: t('iot.route.device.dvcGrpIdText'),
    field: 'DvcGrpId',
    width: 180,
    visible: false,
    formVisible: false,
    filterable: true,
    filterIndex: 'Device.DvcGrpId',
    dataType: 'integer',
    formRender: {
      name: 'ApiTreeSelect',
      props: {
        api: getDeviceGroupTreeNodeExclude,
        // use name as label
        labelField: 'Name',
        // use id as value
        valueField: 'Id',
        showSearch: true,
        childrenField: 'Items',
        treeNodeFilterProp: 'Name',
        treeDefaultExpandAll: true,
        // not request untill to select
        immediate: true,
      },
    },
  },
  {
    title: t('iot.route.device.dvcGrpNameText'),
    field: 'DvcGrpName',
    width: 200,
    visible: false,
    formVisible: false,
    filterable: true,
    filterIndex: 'Device.DvcGrp.Name',
    dataType: 'string',
  },
  {
    title: t('iot.route.device.idText'),
    field: 'DeviceId',
    width: 180,
    visible: false,
    formVisible: true,
    filterable: true,
    dataType: 'integer',
    formRender: {
      name: 'ApiSelect',
      props: {
        api: readDeviceNodes,
        // use name as label
        labelField: 'Name',
        // use id as value
        valueField: 'Id',
        showSearch: true,
        optionFilterProp: 'Name',
        // not request untill to select
        immediate: true,
      },
    },
  },
  {
    title: t('iot.route.device.nameText'),
    field: 'DeviceName',
    width: 200,
    visible: false,
    formVisible: false,
    filterable: true,
    filterIndex: 'Device.Name',
    dataType: 'string',
  },
  {
    title: t('iot.route.device.config.nameText'),
    field: 'Name',
    width: 150,
    formVisible: true,
    required: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('iot.route.device.config.descriptionText'),
    field: 'Description',
    width: 200,
    formVisible: true,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('iot.route.device.config.valueText'),
    field: 'Value',
    width: 200,
    formVisible: true,
    filterable: true,
    dataType: 'string',
    formatter: ({ row }) => {
      try {
        if (row.ValidValues && Object.keys(row.ValidValues).length > 0) {
          return row.ValidValues[row.Value];
        } else {
          return row.Value;
        }
      } catch {
        return row.Value;
      }
    },
  },
  {
    title: t('iot.route.device.config.orderNumberText'),
    field: 'OrderNumber',
    width: 80,
    filterable: false,
    dataType: 'number',
  },
  {
    title: t('iot.route.device.config.validValuesText'),
    field: 'ValidValues',
    width: 0,
    visible: false,
    formVisible: true,
    filterable: false,
    dataType: 'text',
    formCustomer: {
      colProps: {
        // span: 24,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 24, // ≥768px
        lg: 24, // ≥992px
        xl: 24, // ≥1200px
        xxl: 24, // ≥1600px
        // xxxl: 24, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('iot.route.device.config.remarkText'),
    field: 'Remark',
    width: 200,
    formVisible: true,
    filterable: true,
    dataType: 'text',
    formCustomer: {
      colProps: {
        // span: 24,
        xs: 24, // <576px
        sm: 24, // ≥576px
        md: 24, // ≥768px
        lg: 24, // ≥992px
        xl: 24, // ≥1200px
        xxl: 24, // ≥1600px
        // xxxl: 24, // ≥2000px 3.0
      },
    },
  },
  {
    title: t('entity.creationAudited.creatorIdTitle'),
    field: 'CreatorId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.creatorNameTitle'),
    field: 'CreatorName',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.creatorNicknameTitle'),
    field: 'CreatorNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'CreatorId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.creationAudited.createdTimeTitle'),
    field: 'CreatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
  {
    title: t('entity.updateAudited.updaterIdTitle'),
    field: 'UpdaterId',
    width: 0,
    visible: false,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updaterNameTitle'),
    field: 'UpdaterName',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'UserName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updaterNicknameTitle'),
    field: 'UpdaterNickname',
    width: 100,
    formVisible: true,
    formEditable: false,
    filterable: true,
    filterIndex: 'UpdaterId',
    filterJoin: {
      entityType: 'EaCloud.Pack.Identity.Entities.User,EaCloud.Pack.Identity',
      filterField: 'NickName',
      selectField: 'Id',
    },
    dataType: 'string',
  },
  {
    title: t('entity.updateAudited.updatedTimeTitle'),
    field: 'UpdatedTime',
    width: 180,
    formVisible: true,
    formEditable: false,
    filterable: true,
    dataType: 'datetime',
  },
];
